package com.zzw.system.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**   监听器分配
 * @author zzw
 * @date 2023/4/28 15:05
 */

//Event的选项包含：
//
//        Create：任务创建后触发
//        Assignment：任务分配后触发
//        Delete：任务完成后触发
//        All：所有事件发生都触发
//        定义任务监听类，且类必须实现 org.activiti.engine.delegate.TaskListener 接口

//    在模型中加入所需事件，和自己的监听类，到时会自动导入
@SpringBootTest
public class ProcessTestDemo3 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;
//    流程部署定义
    @Test
    public void deployProcess(){
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("process/jiaxin.bpmn20.xml").name("加薪申请流程").deploy();
        System.out.println("流程部署id:"+deployment.getId());
        System.out.println("流程部署名："+deployment.getName());
        System.out.println("流程部署key："+deployment.getKey());

    }
//    创建流程实例
    @Test
    public void startUpProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaxin");
        System.out.println("流程定义id："+processInstance.getProcessDefinitionId());
        System.out.println("流程实例id："+processInstance.getId());
    }
//     查询当前个人待执行的任务
    @Test
    public void findPendingTaskList(){
        List<Task> taskList = taskService.createTaskQuery().taskAssignee("aaa").list();
        for (Task task : taskList) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }

}
